{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Add to Array-Form of Integer"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #数学"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: addToArrayForm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #数组形式的整数加法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>整数的 <strong>数组形式</strong> &nbsp;<code>num</code>&nbsp;是按照从左到右的顺序表示其数字的数组。</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>例如，对于 <code>num = 1321</code> ，数组形式是 <code>[1,3,2,1]</code> 。</li>\n",
    "</ul>\n",
    "\n",
    "<p>给定 <code>num</code> ，整数的 <strong>数组形式</strong> ，和整数 <code>k</code> ，返回 <em>整数 <code>num + k</code> 的 <strong>数组形式</strong></em> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<ol>\n",
    "</ol>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>num = [1,2,0,0], k = 34\n",
    "<strong>输出：</strong>[1,2,3,4]\n",
    "<strong>解释：</strong>1200 + 34 = 1234\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>num = [2,7,4], k = 181\n",
    "<strong>输出：</strong>[4,5,5]\n",
    "<strong>解释：</strong>274 + 181 = 455\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>num = [2,1,5], k = 806\n",
    "<strong>输出：</strong>[1,0,2,1]\n",
    "<strong>解释：</strong>215 + 806 = 1021\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= num.length &lt;= 10<sup>4</sup></code></li>\n",
    "\t<li><code>0 &lt;= num[i] &lt;= 9</code></li>\n",
    "\t<li><code>num</code>&nbsp;不包含任何前导零，除了零本身</li>\n",
    "\t<li><code>1 &lt;= k &lt;= 10<sup>4</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [add-to-array-form-of-integer](https://leetcode.cn/problems/add-to-array-form-of-integer/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [add-to-array-form-of-integer](https://leetcode.cn/problems/add-to-array-form-of-integer/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,2,0,0]\\n34', '[2,7,4]\\n181', '[2,1,5]\\n806']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        t = 0\n",
    "        for i in range(len(num)-1, -1, -1):\n",
    "            t += k % 10\n",
    "            k = k // 10\n",
    "            num[i] += t\n",
    "            t = num[i] // 10\n",
    "            num[i] = num[i] % 10\n",
    "        k += t\n",
    "        while k > 0:\n",
    "            num.insert(0, k % 10)\n",
    "            k //= 10\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "\r\n",
    "    def addToArrayForm(self, num: list[int], k: int) -> list[int]:\r\n",
    "        return list(map(int, str(int(''.join(map(str, num))) + k)))\r\n",
    "\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        sys.set_int_max_str_digits(1000000000)\n",
    "        return list(map(int,str(int(''.join(map(str,num))) + k)))\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        \n",
    "        \n",
    "        return list(map(int, str(int(\"\".join(map(str, num))) + k)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: list[int], k: int):\n",
    "        l = num\n",
    "        i = int(\"\".join([str(i) for i in l]))\n",
    "        x=i+k\n",
    "        result=[]\n",
    "        for i in str(x):\n",
    "            result.append(i)\n",
    "        results = list(map(int, result))\n",
    "        return results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        # no need to convert k into list\n",
    "        # no need to create a new list, update the num directly\n",
    "        carry=0\n",
    "        #travsal the list first\n",
    "        for i in range(len(num)-1,-1,-1):\n",
    "            tmp=k%10 # get the last number of K\n",
    "            s=num[i]+tmp+carry\n",
    "            if s <10:\n",
    "                num[i]=s\n",
    "                carry=0\n",
    "            elif s>=10:\n",
    "                num[i]=s%10\n",
    "                carry=1\n",
    "            k//=10 # pop out the last number of K\n",
    "\n",
    "        while k > 0:\n",
    "            s=k%10 + carry\n",
    "            if s>=10:\n",
    "                carry=1\n",
    "                s=s%10  \n",
    "            else:\n",
    "                carry=0\n",
    "            num.insert(0,s)\n",
    "            k//=10\n",
    "        if carry==1:\n",
    "            num.insert(0,1)\n",
    "            \n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        # no need to convert k into list\n",
    "        # no need to create a new list, update the num directly\n",
    "        carry=0\n",
    "        #travsal the list first\n",
    "        for i in range(len(num)-1,-1,-1):\n",
    "            # tmp=k%10 # get the last number of K\n",
    "            carry,num[i]=divmod(num[i]+k%10+carry,10) #求和取余数一步搞定\n",
    "            # carry=s//10\n",
    "            # num[i]=s if s<10 else s%10\n",
    "\n",
    "            k//=10 # pop out the last number of K\n",
    "\n",
    "        while k > 0 or carry ==1:\n",
    "            # s=k%10 + carry\n",
    "            # carry=s//10\n",
    "            carry, number = divmod(k%10+carry,10)\n",
    "            # if s >= 10:\n",
    "            #     s=s%10\n",
    "            num.insert(0,number) # insert to first one\n",
    "            k//=10\n",
    "\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "\n",
    "        '按位相加，list保存'\n",
    "        # k = str(k)\n",
    "        # i, j = len(num)-1, len(k)-1\n",
    "        # add = 0\n",
    "        # ans = []\n",
    "        # while i >= 0 or j >= 0 or add != 0:\n",
    "        #     x = num[i] if i >= 0 else 0\n",
    "        #     y = int(k[j]) if j >= 0 else 0\n",
    "        #     result = x + y + add\n",
    "        #     ans.append(result % 10)\n",
    "        #     add = result // 10\n",
    "        #     i -= 1\n",
    "        #     j -= 1\n",
    "        # return ans[::-1]\n",
    "\n",
    "        '直接加'\n",
    "        i = len(num) - 1\n",
    "        while k:\n",
    "            num[i] += k # 用当前位置 直接加上 整个 k\n",
    "            k, num[i] = num[i] // 10, num[i] % 10  # 再保留新当前位的同时 缩小k的范围\n",
    "            i -= 1\n",
    "\n",
    "            if i < 0 and k:\n",
    "                num.insert(0,0)  # 如果数组遍历完了 k还有 则 num往前 扩容 \n",
    "                i = 0\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        string = int(''.join([str(s) for s in num]))\n",
    "        a = eval(f\"{string}+{str(k)}\")\n",
    "        b = []\n",
    "        c = str(a)\n",
    "        for i in c:\n",
    "            b.append(i)\n",
    "        results = list(map(int, b))\n",
    "\n",
    "\n",
    "        return results\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, nums: List[int], k: int) -> List[int]:\n",
    "        i = len(nums) - 1\n",
    "        while k:\n",
    "            nums[i] += k\n",
    "            k, nums[i] = nums[i] // 10, nums[i] % 10\n",
    "            i -= 1\n",
    "            if i < 0 and k:\n",
    "                nums.insert(0, 0)\n",
    "                i = 0\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\n",
    "    def addToArrayForm(self, A: List[int], K: int) -> List[int]:\n",
    "\n",
    "        i = len(A) - 1\n",
    "\n",
    "        while K:\n",
    "\n",
    "            A[i] += K\n",
    "\n",
    "            K, A[i] = A[i] // 10, A[i] % 10\n",
    "\n",
    "            i -= 1\n",
    "\n",
    "\n",
    "\n",
    "            if i < 0 and K:\n",
    "\n",
    "                A.insert(0,0)\n",
    "\n",
    "                i = 0\n",
    "\n",
    "        return A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        # no need to convert k into list\n",
    "        # no need to create a new list, update the num directly\n",
    "        carry=0\n",
    "        #travsal the list first\n",
    "        for i in range(len(num)-1,-1,-1):\n",
    "            # tmp=k%10 # get the last number of K\n",
    "            carry,num[i]=divmod(num[i]+k%10+carry,10) #求和取余数一步搞定\n",
    "            # carry=s//10\n",
    "            # num[i]=s if s<10 else s%10\n",
    "\n",
    "            k//=10 # pop out the last number of K\n",
    "\n",
    "        while k > 0 or carry ==1:\n",
    "            # s=k%10 + carry\n",
    "            # carry=s//10\n",
    "            carry, number = divmod(k%10+carry,10)\n",
    "            # if s >= 10:\n",
    "            #     s=s%10\n",
    "            num.insert(0,number) # insert to first one\n",
    "            k//=10\n",
    "\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        i = len(num) - 1\n",
    "        while i > -1 and k != 0:\n",
    "            tmp = num[i] + k % 10\n",
    "            num[i] = tmp % 10\n",
    "            k = k // 10 + tmp // 10\n",
    "            i -= 1\n",
    "        while k:\n",
    "            num = [k % 10] + num\n",
    "            k //= 10\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    # def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "    #     x = 0\n",
    "    #     size = len(num) - 1\n",
    "    #     for i in range(len(num)):\n",
    "    #         x += num[i] * pow(10, size - i)   # 当数组长度很大时数据会溢出\n",
    "    #     x += k\n",
    "    #     print(x)\n",
    "    #     ans = []\n",
    "    #     while (x):\n",
    "    #         y = x % 10\n",
    "    #         x //= 10\n",
    "    #         ans.insert(0, y)\n",
    "    #     return ans\n",
    "    # 直接在数组层面上操作\n",
    "     def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        size = len(num) - 1\n",
    "        add = 0\n",
    "        while ((k or add) and size >= 0):   # 避免数组进位导致索引变为-1\n",
    "            x = k % 10\n",
    "            k //= 10\n",
    "            y = num[size] + x + add\n",
    "            num[size] = y % 10\n",
    "            add = y // 10\n",
    "            size -= 1\n",
    "        while (k or add):   # 如果进位或者k没用完，意味着数组进位了\n",
    "            x = k % 10\n",
    "            k //= 10\n",
    "            y = x + add\n",
    "            num.insert(0, y % 10)\n",
    "            add = y // 10\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        # sum = 0\n",
    "        # for i in num:\n",
    "        #     sum = sum * 10 + i\n",
    "        # sum = sum + k\n",
    "        # res = []\n",
    "        # while sum:\n",
    "        #     res.append(sum % 10)\n",
    "        #     sum //= 10\n",
    "        # res = res[::-1]\n",
    "        # return res\n",
    "\n",
    "        i = len(num) - 1\n",
    "        while k:\n",
    "            k += num[i]\n",
    "            num[i],k = k % 10,k // 10\n",
    "            i -= 1\n",
    "\n",
    "            if i < 0 and k:\n",
    "                num.insert(0,0)\n",
    "                i = 0\n",
    "        return num\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        cur = len(num)\n",
    "        while k > 0:\n",
    "            cur -= 1\n",
    "\n",
    "            if cur < 0:\n",
    "                num.insert(0,k)\n",
    "                cur = 0\n",
    "            else:\n",
    "                num[cur] += k\n",
    "            k, num[cur] = divmod(num[cur], 10)\n",
    "            \n",
    "        return num\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        return list(map(int,list(str(int(''.join(list(map(str,num)))) + k))))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        i=len(num)-1\n",
    "        while i>=0 and k!=0:\n",
    "            s=num[i]+k%10\n",
    "            num[i]=s%10\n",
    "            k=k//10+s//10\n",
    "            i-=1\n",
    "        while k:\n",
    "            num=[k%10]+num\n",
    "            k=k//10\n",
    "        return num\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        # no need to convert k into list\n",
    "        # no need to create a new list, update the num directly\n",
    "        carry=0\n",
    "        #travsal the list first\n",
    "        for i in range(len(num)-1,-1,-1):\n",
    "            tmp=k%10 # get the last number of K\n",
    "            s=num[i]+tmp+carry\n",
    "            carry=s//10\n",
    "            num[i]=s if s<10 else s%10\n",
    "\n",
    "            k//=10 # pop out the last number of K\n",
    "\n",
    "        while k > 0 or carry ==1:\n",
    "            s=k%10 + carry\n",
    "            carry=s//10\n",
    "            if s >= 10:\n",
    "                s=s%10\n",
    "            num.insert(0,s) # insert to first one\n",
    "            k//=10\n",
    "\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        n1,l = 0,len(num)\n",
    "        for i in range(l-1):\n",
    "            n1 += num[i]\n",
    "            n1 *= 10\n",
    "        n2 = n1 + num[-1] + k\n",
    "        ans = []\n",
    "        while n2:\n",
    "            ans = [n2 % 10] + ans\n",
    "            n2 //= 10\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        i = len(num) - 1\n",
    "        while i > -1 and k != 0:\n",
    "            tmp = num[i] + k % 10\n",
    "            num[i] = tmp % 10\n",
    "            k = k // 10 + tmp // 10\n",
    "            i -= 1\n",
    "        while k:\n",
    "            num = [k % 10] + num\n",
    "            k //= 10\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self,A:List[int],K:int)->List[int]:\n",
    "\n",
    "        #将整数K转换为字符串，然后将其每个字符转换为整数并存储在列表K中\n",
    "        K = list(map(int,str(K)))\n",
    "\n",
    "        # 创建一个空列表res用于存储结果，初始化索引i和j以分别从A和K中处理数字\n",
    "        res=[]\n",
    "        i,j = len(A)-1,len(K)-1\n",
    "        carry=0\n",
    "\n",
    "        #通过循环遍历A和K来执行相加操作\n",
    "        while i>=0 and j>=0:\n",
    "\n",
    "            #将A[i]、K[j]、carry相加，并将结果添加到res中\n",
    "            res.append(A[i]+K[j]+carry)\n",
    "\n",
    "            #将res的最有一位限制在0-9之间，将carry更新为res除以10的商\n",
    "            res[-1],carry=res[-1]%10,res[-1]//10\n",
    "            i-=1\n",
    "            j-=1\n",
    "\n",
    "        #处理剩余的A中的数字    \n",
    "        while i>=0:\n",
    "            res.append(A[i]+carry)\n",
    "            res[-1],carry=res[-1]%10,res[-1]//10\n",
    "            i-=1\n",
    "        \n",
    "        #处理剩余的K中的数字\n",
    "        while j>=0:\n",
    "            res.append(K[j]+carry)\n",
    "            res[-1],carry=res[-1]%10,res[-1]//10\n",
    "            j-=1\n",
    "\n",
    "        #如果还有进位（carry大于0），则将其添加到结果中    \n",
    "        if carry:\n",
    "            res.append(1)\n",
    "\n",
    "        #返回结果列表，但需要将其逆序以得到正确的顺序\n",
    "        return res[::-1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        i= len(num)-1\n",
    "        while k:\n",
    "            num[i] +=k\n",
    "            k,num[i] = num[i] //10,num[i]%10\n",
    "            i-=1\n",
    "            if i<0 and k:\n",
    "                num.insert(0,0)\n",
    "                i=0\n",
    "        return num\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        # no need to convert k into list\n",
    "        # no need to create a new list, update the num directly\n",
    "        carry=0\n",
    "        #travsal the list first\n",
    "        for i in range(len(num)-1,-1,-1):\n",
    "            tmp=k%10 # get the last number of K\n",
    "            s=num[i]+tmp+carry\n",
    "            carry=s//10\n",
    "            num[i]=s if s<10 else s%10\n",
    "\n",
    "            k//=10 # pop out the last number of K\n",
    "\n",
    "        while k > 0:\n",
    "            s=k%10 + carry\n",
    "            carry=s//10\n",
    "            if s >= 10:\n",
    "                s=s%10\n",
    "            num.insert(0,s) # insert to first one\n",
    "            k//=10\n",
    "        # add the last 1 to the first \n",
    "        if carry==1:\n",
    "            num.insert(0,1)\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        i=len(num)-1\n",
    "        while i>=0 and k!=0:\n",
    "            s=num[i]+k%10\n",
    "            num[i]=s%10\n",
    "            k=k//10+s//10\n",
    "            i-=1\n",
    "        while k:\n",
    "            num=[k%10]+num\n",
    "            k=k//10\n",
    "        return num\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        i = len(num) - 1\n",
    "        while i > -1:\n",
    "            tmp = num[i] + k % 10\n",
    "            num[i] = tmp % 10\n",
    "            k = k // 10 + tmp // 10\n",
    "            i -= 1\n",
    "        while k:\n",
    "            num = [k % 10] + num\n",
    "            k //= 10\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        res = []\n",
    "        i = len(num) - 1\n",
    "        jinwei = 0\n",
    "        if i==-1:\n",
    "            while k!=0:\n",
    "                res.append(k%10)\n",
    "                k//=10\n",
    "        while i != -1 or k != 0:\n",
    "            if i != -1 and k != 0:\n",
    "                he = num[i] + k % 10 + jinwei\n",
    "                if he < 10:\n",
    "                    res.append(he)\n",
    "                    jinwei = 0\n",
    "                else:\n",
    "                    res.append(he - 10)\n",
    "                    jinwei = 1\n",
    "                i -= 1\n",
    "                k //= 10\n",
    "            if i != -1 and k==0:\n",
    "                he = num[i] + jinwei\n",
    "                if he < 10:\n",
    "                    res.append(he)\n",
    "                    jinwei = 0\n",
    "                else:\n",
    "                    res.append(he - 10)\n",
    "                    jinwei = 1\n",
    "                i -= 1\n",
    "            if i==-1 and k!=0:\n",
    "                he = k % 10 + jinwei\n",
    "                if he < 10:\n",
    "                    res.append(he)\n",
    "                    jinwei = 0\n",
    "                else:\n",
    "                    res.append(he - 10)\n",
    "                    jinwei = 1\n",
    "                k//=10\n",
    "        if jinwei == 1:\n",
    "            res.append(1)\n",
    "        res.reverse()\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        i=len(num)-1\n",
    "        while k:\n",
    "            num[i]+=k\n",
    "            k,num[i]=num[i]//10,num[i]%10\n",
    "            i-=1\n",
    "            if i<0 and k:\n",
    "                num.insert(0,0)\n",
    "                i=0\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        n = len(num)\n",
    "        m = n - 1\n",
    "\n",
    "        res = 0\n",
    "\n",
    "        for i in range(n):\n",
    "            res += 10 ** m * num[i]\n",
    "            m -= 1\n",
    "\n",
    "        res += k\n",
    "        ans = []\n",
    "        while res != 0:\n",
    "            ans.append(res % 10)\n",
    "            res //= 10\n",
    "\n",
    "        ans.reverse()\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        number = 0\n",
    "        for d in num:\n",
    "            number = number * 10 + d\n",
    "        \n",
    "        number += k\n",
    "\n",
    "        res = []\n",
    "        while number > 0:\n",
    "            res.append(number % 10)\n",
    "            number //= 10\n",
    "        \n",
    "        res = list(reversed(res))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        ans=0\n",
    "        for x in num:\n",
    "            ans = 10*ans +x\n",
    "        ans+=k\n",
    "        res=[]\n",
    "        while ans:\n",
    "            a=ans%10\n",
    "            res.append(a)\n",
    "            ans =ans//10\n",
    "        return res[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        dig=0\n",
    "        for i in range(len(num)):\n",
    "            dig=dig*10+num[i]\n",
    "        print(dig)\n",
    "        ans=dig+k\n",
    "        sys.set_int_max_str_digits(20000)\n",
    "        res=[]\n",
    "        for c in str(ans):\n",
    "            res.append(int(c))\n",
    "        print(res)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "sys.set_int_max_str_digits(100000) \n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        num_st=\"\"\n",
    "        for i in num:\n",
    "            num_st=\"\".join((num_st,str(i)))\n",
    "        res=str(int(num_st)+k)\n",
    "        return [int(i) for i in res]\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, A: List[int], K: int) -> List[int]:\n",
    "        i = len(A) - 1\n",
    "        while K:\n",
    "            A[i] += K\n",
    "            K, A[i] = A[i] // 10, A[i] % 10\n",
    "            i -= 1\n",
    "            if i < 0 and K:\n",
    "                A.insert(0,0)\n",
    "                i = 0\n",
    "        return A\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        n = len(num)\n",
    "        ret = 0\n",
    "        ans = []\n",
    "        for i in range(len(num)):\n",
    "            ret += num[i]*10**(n-i-1)\n",
    "        ret += k\n",
    "        while ret:\n",
    "            ans.append(ret%10)\n",
    "            ret = ret // 10\n",
    "        return ans[::-1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        n1,l = 0,len(num)\n",
    "        for i in range(l-1):\n",
    "            n1 += num[i]\n",
    "            n1 *= 10\n",
    "        n2 = n1 + num[-1] + k\n",
    "        ans = []\n",
    "        while n2:\n",
    "            ans.append(n2 % 10)\n",
    "            n2 //= 10\n",
    "        return ans[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        n = len(num)\n",
    "        m = n - 1\n",
    "\n",
    "        res = 0\n",
    "\n",
    "        for i in range(n):\n",
    "            res += 10 ** m * num[i]\n",
    "            m -= 1\n",
    "\n",
    "        res += k\n",
    "        ans = []\n",
    "        while res != 0:\n",
    "            ans.append(res % 10)\n",
    "            res //= 10\n",
    "\n",
    "        ans.reverse()\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, A: List[int], K: int) -> List[int]:\n",
    "        K = list(map(int,str(K)))\n",
    "        \n",
    "        res = []\n",
    "        i,j = len(A)-1,len(K)-1\n",
    "        carry = 0\n",
    "\n",
    "        while i >= 0 and j >= 0:\n",
    "            res.append(A[i] + K[j] + carry)\n",
    "            res[-1],carry = res[-1] % 10, res[-1] // 10\n",
    "            i -= 1\n",
    "            j -= 1\n",
    "        while i >= 0:\n",
    "            res.append(A[i] + carry)\n",
    "            res[-1],carry = res[-1] % 10, res[-1] // 10\n",
    "            i -= 1\n",
    "        while j >= 0:\n",
    "            res.append(K[j] + carry)\n",
    "            res[-1],carry = res[-1] % 10, res[-1] // 10\n",
    "            j -= 1\n",
    "\n",
    "        if carry:\n",
    "            res.append(1)\n",
    "\n",
    "        return res[::-1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, A: List[int], K: int) -> List[int]:\n",
    "        K = list(map(int,str(K)))\n",
    "        \n",
    "        res = []\n",
    "        i,j = len(A)-1,len(K)-1\n",
    "        carry = 0\n",
    "\n",
    "        while i >= 0 and j >= 0:\n",
    "            res.append(A[i] + K[j] + carry)\n",
    "            res[-1],carry = res[-1] % 10, res[-1] // 10\n",
    "            i -= 1\n",
    "            j -= 1\n",
    "        while i >= 0:\n",
    "            res.append(A[i] + carry)\n",
    "            res[-1],carry = res[-1] % 10, res[-1] // 10\n",
    "            i -= 1\n",
    "        while j >= 0:\n",
    "            res.append(K[j] + carry)\n",
    "            res[-1],carry = res[-1] % 10, res[-1] // 10\n",
    "            j -= 1\n",
    "\n",
    "        if carry:\n",
    "            res.append(1)\n",
    "\n",
    "        return res[::-1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        lst = [int(c) for c in str(k)][::-1]\n",
    "        num = num[::-1]\n",
    "        c = 0\n",
    "        la, lb = len(num), len(lst)\n",
    "        if la >= lb:\n",
    "            lst += [0] * (la - lb)\n",
    "        else:\n",
    "            num += [0] * (lb - la)\n",
    "        lm = max(la, lb)\n",
    "        ans = [0] * lm\n",
    "        for i in range(lm):\n",
    "            s = num[i] + lst[i] + c\n",
    "            ans[i] = s % 10\n",
    "            if s >= 10:\n",
    "                c = 1\n",
    "            else:\n",
    "                c = 0\n",
    "        if c == 1:\n",
    "            ans.append(1)\n",
    "        return ans[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        sys.set_int_max_str_digits(10001)\n",
    "        \n",
    "        n = len(num)\n",
    "        m = n - 1\n",
    "\n",
    "        res = 0\n",
    "\n",
    "        for i in range(n):\n",
    "            res += 10 ** m * num[i]\n",
    "            m -= 1\n",
    "\n",
    "        res += k\n",
    "        return list(map(lambda x: int(x), list(str(res))))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, A: List[int], K: int) -> List[int]:\n",
    "        n=len(A)\n",
    "        res = [0]*n\n",
    "        for i,v in enumerate(A[::-1]):\n",
    "            K+=v\n",
    "            res[i]=K%10\n",
    "            K//=10\n",
    "            if K != 0 and i == n - 1:\n",
    "                res+=list(map(int,str(K)[::-1]))\n",
    "        return res[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "\n",
    "    \tres = []\n",
    "    \ti, carry = len(num) - 1, 0\n",
    "    \twhile i >= 0 or k != 0:\n",
    "        \tx = num[i] if i >= 0 else 0\n",
    "        \ty = k % 10 if k != 0 else 0\n",
    "            \n",
    "        \tsum = x + y + carry\n",
    "        \tres.append(sum % 10)\n",
    "        \tcarry = sum // 10\n",
    "\n",
    "        \ti -= 1\n",
    "        \tk //= 10\n",
    "    \tif carry != 0: res.append(carry)\n",
    "    \treturn res[::-1]\n",
    "    \n",
    "    \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        num = num[::-1]\n",
    "        k = str(k)[::-1]\n",
    "        n_len = len(num)\n",
    "        k_len = len(k)\n",
    "\n",
    "        if n_len < k_len:\n",
    "            for count in range(0, k_len - n_len):\n",
    "                num.append(0)\n",
    "        n_len = len(num)\n",
    "        \n",
    "        \n",
    "        for i in range(0, k_len):\n",
    "            date = num[i] + int(k[i])\n",
    "            if date >= 10:\n",
    "                num[i] = int(str(date)[-1])\n",
    "                if i + 1 == n_len:\n",
    "                    num.append(1)\n",
    "                    break \n",
    "                num[i + 1] += 1\n",
    "            else:\n",
    "                num[i] = date\n",
    "\n",
    "        i = 0\n",
    "        while True:\n",
    "        \n",
    "            if num[i] >= 10:\n",
    "                num[i] = int(str(num[i])[-1])\n",
    "                if i == n_len - 1:\n",
    "                    num.append(1)\n",
    "                    return num[::-1]\n",
    "                num[i + 1] += 1\n",
    "\n",
    "            i += 1\n",
    "\n",
    "            if i == n_len:\n",
    "                return num[::-1]\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def addToArrayForm(self, num, k):\n",
    "        \"\"\"\n",
    "        :type num: List[int]\n",
    "        :type k: int\n",
    "        :rtype: List[int]\n",
    "        \"\"\"\n",
    "        k_str = str(k)\n",
    "        carry = 0\n",
    "        result = []\n",
    "        \n",
    "        i = len(num) - 1\n",
    "        j = len(k_str) - 1\n",
    "\n",
    "        while i >= 0 or j >= 0 or carry:\n",
    "            x = num[i] if i >= 0 else 0\n",
    "            y = int(k_str[j]) if j >= 0 else 0\n",
    "            total = x + y + carry\n",
    "            carry = total // 10\n",
    "            result.append(total % 10)\n",
    "            \n",
    "            i -= 1\n",
    "            j -= 1\n",
    "\n",
    "        return result[::-1]  # Reverse the result to get the correct order"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        # count = 0\n",
    "        # for n in num:\n",
    "        #     count = count * 10 + n\n",
    "        # ret = []\n",
    "        # for c in str(count + k):\n",
    "        #     ret.append(int(c))\n",
    "        # return ret\n",
    "\n",
    "        # 新数字\n",
    "        ad = []\n",
    "        for ch in str(k):\n",
    "            ad.append(int(ch))\n",
    "        # 补齐位数\n",
    "        ln, la = len(num), len(ad)\n",
    "        if ln > la:\n",
    "            ad = [0] * (ln - la) + ad\n",
    "            maxl = ln\n",
    "        elif ln < la:\n",
    "            num = [0] * (la - ln) + num\n",
    "            maxl = la\n",
    "        else:\n",
    "            maxl = ln\n",
    "        #\n",
    "        # l = max(ln, la)\n",
    "        u = 0\n",
    "        ret = []\n",
    "        for i in range(-1, -maxl - 1, -1):\n",
    "            n = int(num[i]) + int(ad[i]) + u\n",
    "            ret.append(n % 10)\n",
    "            u = n // 10\n",
    "        if u == 1:\n",
    "            ret.append(1)\n",
    "        ret.reverse()\n",
    "        return ret\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "\n",
    "        i = len(num) - 1\n",
    "        nxt = 0\n",
    "        ans = []\n",
    "        while k or nxt != 0 or i >= 0:\n",
    "            if k > 0:\n",
    "                cur_k = k % 10\n",
    "            else:\n",
    "                cur_k = 0\n",
    "            if i >= 0:\n",
    "                cur_num = num[i]\n",
    "            else:\n",
    "                cur_num = 0\n",
    "\n",
    "            cur_ans = cur_k + cur_num + nxt\n",
    "            if cur_ans > 9:\n",
    "                cur_ans = cur_ans - 10\n",
    "                ans.append(cur_ans)\n",
    "                nxt = 1\n",
    "            else:\n",
    "                ans.append(cur_ans)\n",
    "                nxt = 0\n",
    "\n",
    "            k = k // 10\n",
    "            i -= 1\n",
    "        return ans[::-1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "def list2int(lst: List[int]) -> int:\n",
    "    res: int = 0\n",
    "    for i in range(len(lst)):\n",
    "        res += 10 ** i * lst[-i-1]\n",
    "    return res\n",
    "\n",
    "\n",
    "def int2list(integer: int) -> List[int]:\n",
    "    res: List[int] = []\n",
    "    while integer != 0:\n",
    "        temp = integer // 10\n",
    "        res.append(integer - temp * 10)\n",
    "        integer = temp\n",
    "    return res[::-1]\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        return int2list(list2int(num) + k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        # num_k = list(map(int, list(str(k))))[::-1]\n",
    "        # num = num[::-1]\n",
    "        # i = m = 0\n",
    "        # res = []\n",
    "        # while i < len(num) or i < len(num_k) or m:\n",
    "        #     m += num[i] if i < len(num) else 0\n",
    "        #     m += num_k[i] if i < len(num_k) else 0\n",
    "        #     res.append(m%10)\n",
    "        #     m //= 10\n",
    "        #     i += 1\n",
    "        # return res[::-1]\n",
    "\n",
    "        # ---------------------------------\n",
    "        i, m = len(num)-1, 0\n",
    "        res = []\n",
    "        while i >= 0 or k or m:\n",
    "            m += num[i] if i >= 0 else 0\n",
    "            m += k % 10\n",
    "            res.append(m%10)\n",
    "            m //= 10\n",
    "            k //= 10\n",
    "            i -= 1\n",
    "        return res[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self,A:List[int],K:int)->List[int]:\n",
    "        #将整数K转换为字符串\n",
    "        K = list(map(int,str(K)))\n",
    "        res=[]\n",
    "        i,j = len(A)-1,len(K)-1\n",
    "        carry=0\n",
    "        while i>=0 and j>=0:\n",
    "            res.append(A[i]+K[j]+carry)\n",
    "            res[-1],carry=res[-1]%10,res[-1]//10\n",
    "            i-=1\n",
    "            j-=1\n",
    "        while i>=0:\n",
    "            res.append(A[i]+carry)\n",
    "            res[-1],carry=res[-1]%10,res[-1]//10\n",
    "            i-=1\n",
    "        while j>=0:\n",
    "            res.append(K[j]+carry)\n",
    "            res[-1],carry=res[-1]%10,res[-1]//10\n",
    "            j-=1\n",
    "        if carry:\n",
    "            res.append(1)\n",
    "        return res[::-1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        num_int = 0\n",
    "        for i in range(1, len(num)+1):  # 将 num 列表转换成整数\n",
    "            num_int += num[-i] * pow(10, i-1)\n",
    "        num_int += k  # 与 k 相加\n",
    "        num_list = []\n",
    "        while num_int:  # 再转换成列表\n",
    "            num_list.append(num_int % 10)\n",
    "            num_int = num_int // 10\n",
    "        return num_list[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self,A:List[int],K:int)->List[int]:\n",
    "        #将整数K\n",
    "        K = list(map(int,str(K)))\n",
    "        res=[]\n",
    "        i,j = len(A)-1,len(K)-1\n",
    "        carry=0\n",
    "        while i>=0 and j>=0:\n",
    "            res.append(A[i]+K[j]+carry)\n",
    "            res[-1],carry=res[-1]%10,res[-1]//10\n",
    "            i-=1\n",
    "            j-=1\n",
    "        while i>=0:\n",
    "            res.append(A[i]+carry)\n",
    "            res[-1],carry=res[-1]%10,res[-1]//10\n",
    "            i-=1\n",
    "        while j>=0:\n",
    "            res.append(K[j]+carry)\n",
    "            res[-1],carry=res[-1]%10,res[-1]//10\n",
    "            j-=1\n",
    "        if carry:\n",
    "            res.append(1)\n",
    "        return res[::-1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        #小学加法，从低到高依次计算\n",
    "        k = list(map(int,str(k)))\n",
    "        L1,L2 = len(num)-1,len(k)-1\n",
    "        ans = []\n",
    "        #进位\n",
    "        carry = 0\n",
    "        while L1>=0 and L2>=0:\n",
    "            add = num[L1]+k[L2]+carry\n",
    "            ans.append(add%10)\n",
    "            carry = add//10\n",
    "            L1 -= 1\n",
    "            L2 -= 1\n",
    "        while L1>=0:\n",
    "            add = num[L1]+carry\n",
    "            ans.append(add%10)\n",
    "            carry = add//10\n",
    "            L1 -= 1\n",
    "        while L2>=0:\n",
    "            add = k[L2]+carry\n",
    "            ans.append(add%10)\n",
    "            carry = add//10\n",
    "            L2 -= 1\n",
    "        if carry == 1:\n",
    "            ans.append(carry)\n",
    "        return ans[::-1]\n",
    "        \n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        num.reverse()\n",
    "        n=0\n",
    "        for i in range(len(num)):\n",
    "            n+=num[i]*10**i\n",
    "\n",
    "\n",
    "        import sys\n",
    "        sys.set_int_max_str_digits(40000)\n",
    "\n",
    "        \n",
    "        result = list(str(n+k))\n",
    "\n",
    "        rr = []\n",
    "        for i in range(len(result)):\n",
    "            rr.append(int(result[i]))\n",
    "\n",
    "        return rr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        k = list(map(int,str(k)))\n",
    "        \n",
    "        res = []\n",
    "        i,j = len(num)-1,len(k)-1\n",
    "        carry = 0\n",
    "\n",
    "        while i >= 0 and j >= 0:\n",
    "            res.append(num[i] + k[j] + carry)\n",
    "            res[-1],carry = res[-1] % 10, res[-1] // 10\n",
    "            i -= 1\n",
    "            j -= 1\n",
    "        while i >= 0:\n",
    "            res.append(num[i] + carry)\n",
    "            res[-1],carry = res[-1] % 10, res[-1] // 10\n",
    "            i -= 1\n",
    "        while j >= 0:\n",
    "            res.append(k[j] + carry)\n",
    "            res[-1],carry = res[-1] % 10, res[-1] // 10\n",
    "            j -= 1\n",
    "\n",
    "        if carry:\n",
    "            res.append(1)\n",
    "\n",
    "        return res[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        kList = [eval(x) for x in str(k)]\n",
    "        # print(\"kList: \", kList)\n",
    "\n",
    "        num1 = num[::-1]\n",
    "        kList1 = kList[::-1]\n",
    "\n",
    "        if len(num1) > len(kList1):\n",
    "            num1, kList1 = kList1, num1 \n",
    "\n",
    "        len1 = len(num1)\n",
    "        len2 = len(kList1)\n",
    "        add = 0\n",
    "        i = 0\n",
    "        res = []\n",
    "\n",
    "        while i < len1 or i < len2 or add != 0:\n",
    "            if i< len1:\n",
    "                v = num1[i] + kList1[i] + add\n",
    "            elif i < len2:\n",
    "                v = kList1[i] + add\n",
    "            else:\n",
    "                v = add \n",
    "\n",
    "            if v > 9:\n",
    "                add = 1\n",
    "                v1 = v % 10\n",
    "            else:\n",
    "                add = 0\n",
    "                v1 = v\n",
    "            \n",
    "            res.append(v1)\n",
    "            i += 1\n",
    "        \n",
    "\n",
    "        return res[::-1]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        import sys\n",
    "        sys.set_int_max_str_digits(0)  \n",
    "        return [ int(_) for _ in str(int(\"\".join(str(_) for _ in num))+k) ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        res = []\n",
    "        k1 = [int(x) for x in str(k)] \n",
    "        n1,n2,add = len(num)-1, len(k1)-1, 0\n",
    "        while n1 >= 0 or n2 >= 0:\n",
    "            print(n1,n2)\n",
    "            x = num[n1] if n1 >=0 else 0\n",
    "            y = k1[n2] if n2 >=0 else 0\n",
    "            sum = x+y+add\n",
    "            res.append(sum % 10)\n",
    "            add = sum // 10\n",
    "            n1 = n1 - 1\n",
    "            n2 = n2-1\n",
    "        if add != 0: res.append(add)\n",
    "        return res[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        s=0\n",
    "        anas=[]\n",
    "        num=num[::-1]\n",
    "        m=0\n",
    "        while(m<len(num) and k!=0):\n",
    "            print(m,len(num))\n",
    "            h=(num[m]+k%10+s)%10\n",
    "            s=(num[m]+k%10+s)//10\n",
    "            anas.append(h)\n",
    "            m=m+1\n",
    "            k=k//10\n",
    "        if m!=len(num):\n",
    "            while(m<len(num)):\n",
    "                h=(num[m]+s)%10\n",
    "                s=(num[m]+s)//10\n",
    "                anas.append(h)\n",
    "                m=m+1\n",
    "        if k!=0:\n",
    "            while(k!=0):\n",
    "                h=(k%10+s)%10\n",
    "                s=(k%10+s)//10\n",
    "                anas.append(h)\n",
    "                k=k//10\n",
    "        if s==1:\n",
    "            anas.append(1)\n",
    "        return anas[::-1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "sys.set_int_max_str_digits(100000) \n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        result = int(''.join(map(str, num))) + k\n",
    "        # Convert the result back to a list of digits and return it.\n",
    "        return list(map(int, str(result)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        sys.set_int_max_str_digits(10001)\n",
    "        return list(map(int,str(k+int(''.join(map(str,num))))))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        sys.set_int_max_str_digits(500000)\n",
    "\n",
    "        return list(map(int,str(int(\"\".join(map(str,num))) + k)))\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        # str_num = \"\".join([str(n) for n in num])\n",
    "        # sum_num = int(str_num) + k\n",
    "        # list_num = list(str(sum_num))\n",
    "        # return [int(l) for l in list_num]\n",
    "        # ↑ 上述方法在使用 num 长度大于某个值的情况下就会导致溢出错误\n",
    "        # ValueError: Exceeds the limit (4300) for integer string conversion: value has 10000 digits; use sys.set_int_max_str_digits() to increase the limit\n",
    "\n",
    "        num_k = list(str(k))\n",
    "        num_k = [int(i) for i in num_k]\n",
    "\n",
    "        max_len = max(len(num), len(num_k))\n",
    "        num = [0] * (max_len - len(num)) + num\n",
    "        num_k = [0] * (max_len - len(num_k)) + num_k\n",
    "\n",
    "        res = []\n",
    "        jinwei = 0\n",
    "        while num and num_k:\n",
    "            n1 = num.pop(-1)\n",
    "            n2 = num_k.pop(-1)\n",
    "            \n",
    "            yushu = (n1 + n2 + jinwei) % 10\n",
    "            res.insert(0, yushu)\n",
    "            jinwei = (n1 + n2 + jinwei) // 10\n",
    "        \n",
    "        if jinwei > 0:\n",
    "            res.insert(0, jinwei)\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        sys.set_int_max_str_digits(20000)\n",
    "        str1=\"\".join(map(str,num))\n",
    "        a=int(str1)+k\n",
    "        a=list(map(int,str(a)))\n",
    "        return a\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        sys.set_int_max_str_digits(20000)\n",
    "        return list(map(int, str(int(''.join(map(str, num))) + k)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        sys.set_int_max_str_digits(15000)  # 增加限制为15000位\n",
    "\n",
    "        # 列表合并整数\n",
    "        # s=''\n",
    "        # for i in num:\n",
    "        #     s=s+str(i)  # 字符串\n",
    "        # s=int(s)   # 有限制\n",
    "        num = [str(i) for i in num]\n",
    "        s=int(''.join(num))   # 有限制\n",
    "        res=s+k\n",
    "        # 整数转换为列表\n",
    "        # 一\n",
    "        # res=str(res)\n",
    "        # new_list=[]\n",
    "        # for i in res:\n",
    "        #     new_list.append(int(i))\n",
    "        # 二\n",
    "        # new_list=[int(x) for x in str(res)] \n",
    "        # 三\n",
    "        new_list=[]\n",
    "        while res:\n",
    "            new_list.append(res%10)\n",
    "            res=res//10\n",
    "        return new_list[::-1]\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        import sys\n",
    "        sys.set_int_max_str_digits(20000)\n",
    "        num = int(\"\".join(map(str, num))) + k\n",
    "        return [int(d) for d in str(num)]\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    sys.set_int_max_str_digits(1000000)\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        new_num = int(''.join(map(lambda x: str(x), num)))\n",
    "        str1 = str(new_num+k)\n",
    "        return [int(_) for _ in str1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        sys.set_int_max_str_digits(15000)  # 增加限制为15000位\n",
    "\n",
    "        # 列表合并整数\n",
    "        # s=''\n",
    "        # for i in num:\n",
    "        #     s=s+str(i)  # 字符串\n",
    "        # s=int(s)   # 有限制\n",
    "        num = [str(i) for i in num]\n",
    "        s=int(''.join(num))   # 有限制\n",
    "        res=s+k\n",
    "        res=str(res)\n",
    "        new_list=[]\n",
    "        for i in res:\n",
    "            new_list.append(int(i))\n",
    "        return new_list\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        sys.set_int_max_str_digits(100000) \n",
    "        ans = list(str(int(''.join(list(map(lambda x: str(x), num)))) + k))\n",
    "        return list(map(lambda x: int(x), ans))\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addToArrayForm(self, num: List[int], k: int) -> List[int]:\n",
    "        sys.set_int_max_str_digits(100000) \n",
    "        ans = list(str(int(''.join(list(map(lambda x: str(x), num)))) + k))\n",
    "        return list(map(lambda x: int(x), ans))\n",
    "\n",
    "\n",
    "        "
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
